package com.safety51.bootstrap.demo.domain.example;

import com.alibaba.fastjson.JSON;
import com.safety51.bootstrap.commons.context.UserInfoContext;
import com.safety51.bootstrap.qpaas.CronTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

/**
 * Created by liuyuancheng on 2022/7/20  <br/>
 *
 * @author liuyuancheng
 */
@EnableScheduling
@Component
public class CronExample {

    @Autowired
    private CronTools cronTools;

    /**
     * sft51-commons.yaml  使用cron.entIds的第一个容器执行
     */
//    @Scheduled(initialDelay = 1, fixedDelay = 20000)
    public void testTask() {
        cronTools.applyFirstEntiIdToUserInfoContext();
        System.out.println(JSON.toJSONString(UserInfoContext.getUserInfo()));
    }

    /**
     * cron.entIds配置了多个容器
     * 定时任务需要处理多个容器
     */
//    @Scheduled(initialDelay = 1, fixedDelay = 20000)
    public void testTask2() {
        for (int i = 0; i < cronTools.getEntIds().size(); i++) {
            cronTools.applyToUserInfoContextByIndex(i);
            System.out.println(JSON.toJSONString(UserInfoContext.getUserInfo()));
        }
    }
}
